//
//  StatusCellRetweetedView.swift
//  weibo
//
//  Created by apple on 15/12/20.
//  Copyright © 2015年 LYR. All rights reserved.
//

import UIKit
import SnapKit

//  转发微博  包括正文和配图
class StatusCellRetweetedView: UIView {
    
    //  定义外部约束属性  保存当前的底部约束
    var botttomConstrants: Constraint?

    //MARK:定义外部模型属性
    var retweeted_status: Status? {
        didSet {
           retweetedLabel.text = "@" + (retweeted_status?.user?.name ?? "") + (retweeted_status?.text ?? "")
            
            pictureView.imageURLs = retweeted_status?.imageURLs
            
            //  卸载掉底部的约束
            self.botttomConstrants?.uninstall()
            
            //  根据是否有配图匹配   重新添加约束
            if let urls = retweeted_status?.imageURLs where urls.count > 0 {
                //  有配图的情况下
            self.snp_makeConstraints { (make) -> Void in
                
                // 添加约束  记录底部约束
                self.botttomConstrants = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                
            }
            
            } else {
            // 没有配图视图的情况下
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    // 添加约束  记录底部约束
                    self.botttomConstrants = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                    
                })
            
            }
        }
    }
    
    
    //MARK:重写构造方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 0.97, alpha: 1)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK:设置页面 布局
    private func setupUI() {
        // 正文
        addSubview(retweetedLabel)
        
        //  配图
        addSubview(pictureView)
        
        
        // 正文 添加约束
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(StatusCellMargin)
        }

        // 配图 添加约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(retweetedLabel.snp_left)
            
        }
        
        // 转发微博自动计算行高
        self.snp_makeConstraints { (make) -> Void in
            
            //
        self.botttomConstrants = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        
        }
    }
    
    // MARK:懒加载所有的子视图
    // 正文
    private lazy var retweetedLabel: UILabel = UILabel(title: "@转发 好好学习，天天向上", color: UIColor.darkGrayColor(), fontSize: 14, margin: StatusCellMargin)
    
    // 配图
    private lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
    
    
}
